<template>
	<view class="modify">
		<form @submit="form_submit">
			<view class="modify_list">
				<view class="modify_item">
					<view class="">头像</view>
					<button class="modify_avatar" hover-class="none" open-type="chooseAvatar"
						@chooseavatar="choose_avatar_event" @tap="choose_avatar_event">
						<image :src="(user_data.user.avatar || default_avatar)" mode="widthFix"></image>
					</button>
				</view>
				<view class="modify_item">
					<view class="">姓名</view>
					<input :type="(application_client_type == 'weixin') ? 'nickname' : 'text'" name="nickName"
						:value="user_data.userName || ''" maxlength="16" placeholder-class="cr-grey" class="cr-base"
						placeholder="姓名2~16个字符">
				</view>
				<view class="modify_item">
					<view class="">手机号</view>
					<input type="number" name="phonenumber" :value="user_data.tel || ''" maxlength="11"
						placeholder-class="cr-grey" class="cr-base" placeholder="输入手机号码">
				</view>
				<view class="modify_item">
					<view class="">微信号</view>
					<input type="text" name="wechatNumber" :value="user_data.wechatNumber || ''" maxlength="30"
						placeholder-class="cr-grey" class="cr-base" placeholder="微信号最多30个字符">
				</view>
				<view class="modify_item">
					<view class="">职位</view>
					<input type="text" name="jobTitle" :value="user_data.jobTitle || ''" maxlength="10"
						placeholder-class="cr-grey" class="cr-base" placeholder="职位最多10个字符">
				</view>
				<view class="modify_item">
					<view class="">邮箱</view>
					<input type="text" name="mailBox" :value="user_data.mailBox || ''" maxlength="30"
						placeholder-class="cr-grey" class="cr-base" placeholder="邮箱最多30个字符">
				</view>
				<view class="modify_item">
					<view class="">公司名称</view>
					<input type="text" name="tenantName" :value="user_data.tenantName || ''" maxlength="30"
						placeholder-class="cr-grey" class="cr-base" placeholder="公司名称最多30个字符">
				</view>
				<view class="modify_item">
					<view class="">详细地址</view>
					<input type="text" name="address" :value="user_data.address || ''" maxlength="100"
						placeholder-class="cr-grey" class="cr-base" placeholder="详细地址最多100个字符">
				</view>
			</view>
			<view class="sumbit">
				<button class="sumbit_tj" type="default" form-type="submit" hover-class="none"
					:disabled="form_submit_disabled_status">保存</button>
			</view>
		</form>
	</view>
</template>
<script>
	const app = getApp();
	
	export default {
		data() {
			return {
				data_list_loding_status: 1,
				data_list_loding_msg: '',
				form_submit_disabled_status: false,
				default_avatar:'https://t1.chowtaiking.com.cn/img/ruoyi/2025/03/10/0c987fc6263b49aba28e456ece3ae95b.png',
				user_data: {},
				gender_list: [],
				entryTime:0,
				status:true,
				back:''
			}
		},
		onUnload(){
			this.OnEntryTime();
			if(this.back == '' || this.back == 'my'){
				uni.switchTab({
					url:"/pages/user/user"
				})
			}else{
				uni.navigateTo({
					url:"/pages/user_radar/user_radar?back=OnbackStage"
				})
			}
		},
		onLoad(params) {
			if(typeof(params)!='undefined'){
				this.back = params.back || '';
				var numberId = params.numberId || '';
				if(numberId != ''){
					uni.clearStorageSync();
					app.globalData.data.numberId = numberId;
				}
			}
		},
		methods: {
			init(e) {
				var user = app.globalData.get_user_info(this, "init", "");
				if (user != false) {
					// 用户未绑定用户则转到登录页面
					if (app.globalData.user_is_need_login(user)) {
						uni.navigateTo({
							url:"/pages/login/login?event_callback=init"
						})
						
					} else {
					  this.get_data();
					}
				}
			},
			// 获取数据
			get_data() {
				var self = this;
				let url = app.globalData.get_request_url("card/index");
				uni.request({
					url:url,
					method:"POST",
					data:{},
					success: (res) => {
						if (res.data.code == 200) {
							self.user_data = res.data.data || {};
						}
					}
				})
			},
			// 头像事件
			choose_avatar_event(e) {
				var value = '';
				// #ifdef MP-WEIXIN
				    value = 'weixin';
				// #endif
				// #ifdef MP-ALIPAY
				    value = 'alipay';
				// #endif
				// #ifdef MP-BAIDU
				    value = 'baidu';
				// #endif
				// #ifdef MP-QQ
				    value = 'qq';
				// #endif
				// #ifdef MP-TOUTIAO
				    value = 'toutiao';
				// #endif
				// #ifdef MP-KUAISHOU
				    value = 'kuaishou';
				// #endif
				// #ifdef H5
				    value = 'h5';
				// #endif
				var self = this;
				if (value == 'weixin') {
					self.upload_handle(e.detail.avatarUrl);
				} else {
					uni.chooseImage({
						count: 1,
						success(res) {
							if (res.tempFilePaths.length > 0) {
								self.upload_handle(res.tempFilePaths[0]);
							}
						}
					});
				}
			},
			// 上传处理
			upload_handle(image) {
				var self = this;
				let url = app.globalData.get_request_url("useravatarupload");
				uni.uploadFile({
					url:url,
					filePath: image,
					name: 'file',
					formData: {},
					success: function(res) {
						if (res.statusCode == 200) {
							var data = typeof res.data == 'object' ? res.data : JSON.parse(res.data);
							if (data.code == 200) {
								var temp = self.user_data;
								temp['avatar'] = data.data.oss_id;
								temp.user.avatar = data.data.data;
								self.user_data = temp;
							} else {
								uni.showToast({
									title:data.msg
								})
							}
						}
					}
				});
			},
			// 数据提交
			form_submit(e) {
				// 表单数据
				var form_data = e.detail.value;
				// 头像
				form_data['avatarId'] = this.user_data.avatar || '';
				form_data['userStoreId'] = this.user_data.userStoreId || '';
				form_data['userId'] = this.user_data.userId || '';
				if(form_data.avatar ==''){
					uni.showToast({
						title:"头像不能为空哦!",
						icon:'error'
					})
					return;
				}
				if(form_data.nickName == ''){
					uni.showToast({
						title:"姓名不能为空哦!",
						icon:'error'
					})
					return;
				}
				if(form_data.phonenumber == ''){
					uni.showToast({
						title:"电话不能为空哦!",
						icon:'error'
					})
					return;
				}
				if(form_data.tenantName == ''){
					uni.showToast({
						title:"公司名称不能为空哦!",
						icon:'error'
					})
					return;
				}
				// 数据保存
				this.form_submit_disabled_status = true
				uni.showLoading({
					title: '处理中...'
				});
				let url = app.globalData.get_request_url("card/save");
				uni.request({
					url:url,
					method: 'POST',
					data: form_data,
					dataType: 'json',
					success: res => {
						uni.hideLoading();
						this.form_submit_disabled_status = false
						if (res.data.code == 200) {
							uni.showToast({
								title:res.data.msg,
								duration:2000
							})
							setTimeout(()=>{
								uni.navigateBack()
							},2000)
							// setTimeout(function() {
							// 	if(this.back == '' || this.back == 'my'){
							// 		uni.switchTab({
							// 			url:"/pages/user/user"
							// 		})
							// 	}else{
							// 		uni.navigateTo({
							// 			url:"/pages/user_radar/user_radar?back=OnbackStage"
							// 		})
							// 	}
							// }, 1000);
						} else {
							if (app.globalData.is_login_check(res.data)) {
								uni.showToast({
									title:res.data.msg
								})
							} else {
								uni.showToast({
									title:'提交失败，请重试！'
								})
							}
						}
					},
					fail: () => {
						uni.hideLoading();
						this.form_submit_disabled_status = false
						uni.showToast({
							title:"服务器请求出错"
						})
					}
				});
			},
			OnEntryTime(){
				var radarlog_data = {
					pageUrl:"pages/card_modify/card_modify",
					name:"修改名片页面",
					remark:"修改名片页面停留时间",
					entryTime:this.entryTime,
				}
				uni.request({
					url:app.globalData.get_request_url("radarlog/add"),
					data:radarlog_data,
					method:"POST",
					success: (res) => {
						console.log(res,"修改名片页面停留时间");
					}
				})
			}
		},
		onShow() {
			this.init();
			// 记录用户进来时间
			this.entryTime = Date.now();
		},
		// 分享给朋友
		onShareAppMessage(res) {
			if(res.from === 'button'){
				console.log(res.target,"target");
			}
			return{
				title:app.globalData.data.tenantName,
				path:"pages/card_modify/card_modify?numberId=" + app.globalData.data.numberId,
				mpId:app.globalData.data.appid
			}
		},
		// 分享朋友圈
		// onShareTimeline(res) {
		// 	return{
		// 		title:app.globalData.data.tenantName,
		// 		type:0,
		// 		summary:""
		// 	}
		// },
	}
</script>
<style lang="less">
	.modify_list {
		width: 100%;

		.modify_item {
			height: 50px;
			border-bottom: 1px solid #f5ece0;
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-left: 20px;
			padding-right: 20px;

			.modify_avatar {
				width: 50px;
				height: 50px;
				margin-right: 10px;

				image {
					margin-left: -8px;
					margin-top: 8px;
				}
			}

			image {
				width: 40px;
				height: 40px;
				border-radius: 50%;
			}

			input {
				font-size: 12px;
				width: 100px;
				text-align: center;
			}
		}
	}

	.sumbit {
		display: flex;
		align-items: center;
		justify-content: center;
		padding-top: 50px;

		.sumbit_tj {
			width: 120px;
			height: 40px;
			line-height: 40px;
			text-align: center;
			background-color: #dfc5a2;
			color: white;
		}
	}
</style>